package com.sz.jinzheng.service.impl;

import com.sz.jinzheng.mapper.BillFormMapper;
import com.sz.jinzheng.model.BillForm;
import com.sz.jinzheng.service.BillFormService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
public class BillFormServiceImpl implements BillFormService {
    @Resource
    private BillFormMapper billFormMapper;

    /**
     * 获取账单信息
     *
     * @param billFormId 账单id
     * @return 所有账单信息
     */
    @Override
    public BillForm getbBillForm(Integer billFormId) {
        return billFormMapper.getbBillForm(billFormId);
    }

    @Override
    /**
     * 获取账单信息（第n期）
     * @param billForm
     * @return
     */
    public BillForm getOneBillForm(BillForm billForm) {
        return billFormMapper.getOneBillForm(billForm);
    }

    ;


    /**
     * 获取一共多少期
     *
     * @param borrowerInfoId
     * @return
     */
    public List<BillForm> getCount(Integer borrowerInfoId) {
        return billFormMapper.getCount(borrowerInfoId);
    }

    ;


    /**
     * 计算罚息方法
     *
     * @param billForm 当前期数
     *
     */
    public BillForm latePayment(BillForm billForm) {
        long time = System.currentTimeMillis();//当前的时间
        Date endTime1 = billForm.getEndTime();//用户最后还款时间
        Timestamp createTime = new Timestamp(endTime1.getTime());
        long endTime = createTime.getTime();//用户最后还款时间
        long day = (time - endTime);
        Double amerce=null;//罚息金额
        if (day>0){
            //用户逾期Math.ceil(double a)向上取整
            int overdueDay = (int) Math.ceil(day / ((long) 1000 * (long) 24 * (long) 3600));//逾期天数
            System.out.println("billForm.getCapital() = " + billForm.getCapital());
            System.out.println("billForm.getPenaltyRate() = " + billForm.getPenaltyRate());
            if (billForm.getCapital()==null){
                billForm.setCapital(1.0);
            }
            amerce = billForm.getCapital() * billForm.getPenaltyRate()*overdueDay;
            billForm.setPenaltyInterest(amerce);//传到对象
            billForm.setOverdueDays(overdueDay);
        }
        //罚息=逾期本金*罚息率*逾期天数
        return billForm;
    }
}
